﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Basic;
using Interface;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Client
{
    /// <summary>
    /// 资源管理器
    /// 加载完成回调顺序，与请求顺序保持一致
    /// </summary>
    public class AssetManager
    {
        /// <summary>
        /// 加载指定资源
        /// </summary>
        /// <param name="assetKey">资源标识</param>
        /// <param name="callback"></param>
        public void LoadGameObjectAsync(string assetKey, Action<GameObject> callback)
        {
            Log.Debug("AssetManager.LoadGameObjectAsync {0}", assetKey);

            var assetPath = assetKey;
#if UNITY_EDITOR
            if (assetKey == "UIWindowLogin")
            {
                assetPath = "Assets/Bundles/UI/Prefab/Login/UIWindowLogin.prefab";
            }
            AsyncActionHelper.Instance.DoActionNextFrame(() =>
            {
                var obj = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
                if (obj == null)
                {
                    Log.Error("AssetManager.LoadGameObjectAsync loaded obj is null: {0} {1}", assetKey, assetPath);
                }
                else
                {
                    obj = GameObject.Instantiate<GameObject>(obj);
                }

                try
                {
                    if (callback != null)
                    {
                        callback(obj);
                    }
                    else
                    {
                        Log.Error("AssetManager.LoadGameObjectAsync callback is null: {0} {1}", assetKey, assetPath);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("AssetManager.LoadGameObjectAsync callback get exception: {0} {1} {2}", assetKey, assetPath, ex);
                }
            });
#endif
        }

        /// <summary>
        /// 资源使用完毕
        /// </summary>
        /// <param name="obj"></param>
        public void OnGameObjectUseEnd(GameObject obj)
        {
            GameObject.Destroy(obj);
        }

        public void AddRefAtlas(string nameAtlas, GameObject go)
        {

        }

        public void SubRefAtlas(string nameAtlas, GameObject go)
        {

        }
    }
}
